﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AllDemo
{
    public partial class 检测两个圆的位置关系 : Form
    {
        public 检测两个圆的位置关系()
        {
            InitializeComponent();
            c1.X = 150;
            c1.Y = 150;
            c2.X = 300;
            c2.Y = 300;
            this.DoubleBuffered = true;
            label2.ForeColor = Color.White;
        }

        private PointF c1 = new PointF();
        private int r1 = 50;
        private PointF c2 = new PointF();
        private PointF currentPoint = new PointF();
        private int r2 = 80;
        private   Graphics g;
        private Color currentColor = Color.Green;
        private bool leftButtonIsEnter = false;
        private bool isInCircle = false;
        private void drawCircle()
        {
            g.SmoothingMode = SmoothingMode.HighQuality;  //图片柔顺模式选择
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量
            g.CompositingQuality = CompositingQuality.HighQuality;//再加一点
            g.FillEllipse(new SolidBrush(Color.Orange), c1.X - r1, c1.Y - r1, 2 * r1, 2 * r1);
            g.FillEllipse(new SolidBrush(currentColor), c2.X - r2, c2.Y - r2, 2 * r2, 2 * r2);
            g.DrawLine(Pens.Red, c1, c2);
            g.DrawLine(Pens.Red, c2.X, c2.Y, c1.X, c2.Y);
            g.DrawLine(Pens.Red, c1.X, c1.Y, c1.X, c2.Y);
        }



        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            g = e.Graphics;
            drawCircle();
        }

        //判断一点是否在圆上，依据点与圆的位置关系，（x-a)^2+(y-b)^2=r^2 则该点在圆上,>r则在圆外，<r则在圆内
        private bool checkPointIsInCircle(Point _clickPoint)
        {
            float tempR = (_clickPoint.X - c2.X) * (_clickPoint.X - c2.X) + (_clickPoint.Y - c2.Y) * (_clickPoint.Y - c2.Y);
            if (tempR < r2 * r2)
            {
                return true;
            }
            return false;
        }

        private void 检测两个圆的位置关系_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isInCircle = checkPointIsInCircle(e.Location);
                currentPoint = e.Location;
                currentColor = Color.GreenYellow;
                if (isInCircle)
                {
                    Invalidate();
                }
            }
            leftButtonIsEnter = true;
        }

        private void 检测两个圆的位置关系_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (isInCircle)
                {
                    if ((Math.Pow(c1.X - c2.X, 2) + Math.Pow(c1.Y - c2.Y, 2)) > Math.Pow((r1 + r2), 2))
                    {
                        label2.Text = "相离";
                        label2.BackColor = Color.Green;
                        
                    }else if((Math.Pow(c1.X - c2.X, 2) + Math.Pow(c1.Y - c2.Y, 2)) == Math.Pow((r1 + r2), 2))
                    {
                        label2.Text = "相贴";
                    }
                    else
                    {
                        label2.Text = "相交";
                        label2.BackColor = Color.Red;
                    }
                }
                leftButtonIsEnter = false;
                isInCircle = false;
                currentColor = Color.Green;
                Invalidate();
            }
        }

        private void 检测两个圆的位置关系_MouseMove(object sender, MouseEventArgs e)
        {
            if(isInCircle && leftButtonIsEnter)
            {
                
                c2.X = c2.X + (e.X - currentPoint.X);
                c2.Y = c2.Y + (e.Y - currentPoint.Y);
                Invalidate();
                currentPoint = e.Location;
            }
        }
    }
}
